package com.lcxsoft.orderservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcxsoft.commonutils.R;
import com.lcxsoft.feign.clients.GoodsClient;
import com.lcxsoft.feign.clients.UserClient;
import com.lcxsoft.feign.entity.CustomUser;
import com.lcxsoft.feign.entity.Goods;
import com.lcxsoft.feign.entity.School;
import com.lcxsoft.orderservice.entity.Orders;
import com.lcxsoft.orderservice.mapper.OrdersMapper;
import com.lcxsoft.orderservice.query.OrderQuery;
import com.lcxsoft.orderservice.service.OrdersService;
import com.lcxsoft.orderservice.vo.OrderVo;
import com.mysql.cj.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lcx
 * @since 2022-01-22
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Resource
    private OrdersService ordersService;

    @Resource
    private OrdersMapper ordersMapper;

    @Resource
    private GoodsClient goodsClient;

    @Resource
    private UserClient userClient;


    @Override
    public R findOrders(OrderQuery query) {
        //1.根据school找到列表
        //创建page对象
        Page<Orders> ordersPage = new Page<>(query.getCurrentPage(), query.getPageSize());
        //获取分页数据
        if (StringUtils.isNullOrEmpty(query.getSchoolId())){
            ordersService.page(ordersPage, null);
        }else {
            ordersService.page(ordersPage, new LambdaQueryWrapper<Orders>().eq(Orders::getSchoolId, query.getSchoolId()));
        }

        //记录数
        long total = ordersPage.getTotal();
        List<Orders> records = ordersPage.getRecords();
        List<OrderVo> orderVos = new ArrayList<>();
        for (Orders record : records) {
            OrderVo orderVo = new OrderVo();
            BeanUtil.copyProperties(record, orderVo);
            Goods goods = goodsClient.queryGoodsById(record.getGoodsId());
            orderVo.setGoodsName(goods.getGoodsName());
            orderVo.setBuyTime(record.getCreatetime().toString().replace("T", " "));
            School school = userClient.querySchoolBySchoolId(record.getSchoolId());
            orderVo.setSchoolName(school.getSchoolName());
            CustomUser customUser = userClient.queryUserByUserId(record.getUserId());
            orderVo.setUserNickname(customUser.getUserNickname());
            orderVos.add(orderVo);
        }

        return R.ok().data("orders", orderVos).data("total", total);
    }

    @Override
    public R countTodayOrderNumber() {
        String morning = LocalDate.now().toString() + " 00:00:00";
        String night = LocalDate.now().toString() + " 23:59:59";
        Integer count = ordersMapper.countOrdersByTime(morning, night);
        return R.ok().data("todayCount", count);
    }

    @Override
    public R countTodayOrderMoney() {
        String morning = LocalDate.now().toString() + " 00:00:00";
        String night = LocalDate.now().toString() + " 23:59:59";
        BigDecimal bigDecimal = ordersMapper.countOdersMoneyByTime(morning, night);
        return R.ok().data("todayMoney", bigDecimal);
    }

    @Override
    public R queryTodayOrderCountByToken(String schoolId) {
        String morning = LocalDate.now().toString() + " 00:00:00";
        String night = LocalDate.now().toString() + " 23:59:59";
        Integer count = ordersMapper.countOrdersByTimeAndSchoolId(morning, night, schoolId);
        return R.ok().data("todayCount", count);
    }

    @Override
    public R queryTodayOrderMoneyByToken(String schoolId) {
        String morning = LocalDate.now().toString() + " 00:00:00";
        String night = LocalDate.now().toString() + " 23:59:59";
        BigDecimal bigDecimal = ordersMapper.countOdersMoneyByTimeAndSchoolId(morning, night, schoolId);
        return R.ok().data("todayMoney", bigDecimal);
    }
}
